<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="phone"
        placeholder="手机号"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="编号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="渠道" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.channelName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否绑定银行卡" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.bankList.length >= 1">是</span>
          <span v-if="row.bankList.length == 0">否</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="300"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="logOut(row)">
            注销手机号
          </el-button>
          <el-button type="primary" size="mini" @click="unbundle(row)">
            解绑银行卡
          </el-button>
          <el-button type="primary" size="mini" @click="handleDelete(row)">
            注销加解绑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getPhone, deleteUse, unBundle } from "@/api/user";
import Pagination from "@/components/Pagination";
// 用户详情组件
export default {
  components: { Pagination },
  data() {
    return {
      tableData: [],
      phone: "",
    };
  },
  created() {
    this.getPhoneList();
  },
  methods: {
    async getPhoneList() {
      const res = await getPhone({ phone: this.phone });
      this.tableData = res.data.data;
    },
    handleFilter() {
      this.getPhoneList(this.phone);
    },
    unbundle(row) {
      var that = this;
      this.$confirm("解绑提示！", "确认解绑吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        unBundle({ userId: row.userId }).then((res) => {
          if (res.data.code === 200) {
            that.$notify({
              title: "Success",
              message: "解绑成功",
              type: "success",
              duration: 2000,
            });
            that.getPhoneList(this.phone);
          } else {
            this.$notify({
              title: "错误",
              message: res.data.msg,
              type: "fail",
              duration: 2000,
            });
          }
        });
      });
    },
    logOut(row) {
      var that = this;
      this.$confirm("注销提示！", "确认注销吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        deleteUse({ userId: row.userId }).then((res) => {
          if (res.data.code === 200) {
            that.$notify({
              title: "Success",
              message: "注销成功",
              type: "success",
              duration: 2000,
            });
            that.getPhoneList(this.phone);
          } else {
            this.$notify({
              title: "错误",
              message: res.data.msg,
              type: "fail",
              duration: 2000,
            });
          }
        });
      });
    },
    handleDelete(row) {
      var that = this;
      this.$confirm("注销加解绑提示！", "确认注销加解绑除吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        unBundle({ userId: row.userId }).then((res) => {
          if (res.data.code === 200) {
            deleteUse({ userId: row.userId }).then((res) => {
              if (res.data.code === 200) {
                that.$notify({
                  title: "Success",
                  message: "注销加解绑成功",
                  type: "success",
                  duration: 2000,
                });
                that.getPhoneList(this.phone);
              } else {
                this.$notify({
                  title: "错误",
                  message: res.data.msg,
                  type: "fail",
                  duration: 2000,
                });
              }
            });
          } else {
            this.$notify({
              title: "错误",
              message: res.data.msg,
              type: "fail",
              duration: 2000,
            });
          }
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
